package io.terminus.spzx.manager.service.impl;

import io.terminus.spzx.common.config.exception.SpzxCustomException;
import io.terminus.spzx.manager.helper.MenuHelper;
import io.terminus.spzx.manager.mapper.SysMenuMapper;
import io.terminus.spzx.manager.mapper.SysRoleMenuMapper;
import io.terminus.spzx.manager.service.SysMenuService;
import io.terminus.spzx.model.entity.system.SysMenu;
import io.terminus.spzx.model.entity.system.SysUser;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import io.terminus.spzx.model.vo.system.SysMenuVo;
import io.terminus.spzx.utils.AuthContextUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author lichunlong lcl253137@alibaba-inc.com
 * @desc 系统菜单Service实现类
 * @date 2024-07-29
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        //1、查询所有菜单集合
        List<SysMenu> menuList = sysMenuMapper.findAll();

        //2、调用工具类把返回的List集合封装成要求的数据格式
        return MenuHelper.buildTree(menuList);
    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);

        // 新添加子菜单，把父菜单isHalf半开状态: 1
        updateSysRoleMenu(sysMenu);
    }

    /**
     * 新添加子菜单，把父菜单isHalf半开状态 1
     *
     * @param sysMenu 系统菜单
     */
    private void updateSysRoleMenu(SysMenu sysMenu) {
        // 获取当前添加菜单的父菜单
        SysMenu parentMenu = sysMenuMapper.selectParentMenu(sysMenu.getParentId());
        if (Objects.nonNull(parentMenu)) {
            // 把父菜单isHalf半开状态 1
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());
            // 递归调用
            updateSysRoleMenu(parentMenu);
        }
    }

    @Override
    public void update(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }

    @Override
    public void removeById(Long id) {
        // 根据当前菜单id，查询是否包含子菜单
        int count = sysMenuMapper.selectCountById(id);
        // 判断是否包含子菜单
        if (count > 0) {
            throw new SpzxCustomException(ResultCodeEnum.NODE_ERROR);
        }

        sysMenuMapper.delete(id);
    }

    @Override
    public List<SysMenuVo> findMenusByUserId() {
        // 1、获取当前用户id
        SysUser sysUser = AuthContextUtil.get();
        // 2、查询当前用户菜单列表
        List<SysMenu> sysMenuList = MenuHelper.buildTree(sysMenuMapper.findMenuByUserId(sysUser.getId()));
        // 3、List<SysMenu>对象转换成List<SysMenuVo>对象
        return buildMenus(sysMenuList);
    }

    /**
     * 将List<SysMenu>对象转换成List<SysMenuVo>对象
     *
     * @param menuList SysMenu列表
     * @return SysMenuVo列表
     */
    private List<SysMenuVo> buildMenus(List<SysMenu> menuList) {
        List<SysMenuVo> sysMenuVoList = new LinkedList<>();
        for (SysMenu sysMenu : menuList) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
